package com.huaxin.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;

import java.util.Set;

/**
 * Created by Administrator on 2016/11/16.
 */

public class PrefUtil {
    private static String PREF_COMMON = "common.pref";
    private static Context mContext;

    public static void init(Context context) {
        mContext = context;
    }

    public static void set(String key, int value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static void set(String key, boolean value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void set(String key, String value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static boolean get(String key, boolean defValue) {
        return getPreferences().getBoolean(key, defValue);
    }

    public static String get(String key, String defValue) {
        return getPreferences().getString(key, defValue);
    }

    public static int get(String key, int defValue) {
        return getPreferences().getInt(key, defValue);
    }

    public static long get(String key, long defValue) {
        return getPreferences().getLong(key, defValue);
    }

    public static float get(String key, float defValue) {
        return getPreferences().getFloat(key, defValue);
    }

    public static SharedPreferences getPreferences() {
        SharedPreferences pre = mContext.getSharedPreferences(PREF_COMMON,
                0);
        return pre;
    }
    public static SharedPreferences getPreferences(String prefName) {
        return mContext.getSharedPreferences(prefName,
                0);
    }



    /*************************
     * Login Info
     ***********************************/
    private static final String PREF_LOGIN = "login";
    private static final String AUTOLOGIN = "autologin";
    private static final String SAVEPASSWORD = "savepassword";
    private static final String SEARCHKEYWORD = "searchkeyword";

    public static void saveLoginInfo(String name,
                                     String password) {
        SharedPreferences.Editor prefs = mContext.getSharedPreferences(PREF_LOGIN, 0).edit();
        prefs.putString("name", name);
        prefs.putString("password", password);
        prefs.commit();
    }

    public static String[] loadLoginInfo() {
        SharedPreferences prefs = mContext.getSharedPreferences(PREF_LOGIN, 0);
        String[] results = new String[2];
        results[0] = prefs.getString("name", null);
        results[1] = prefs.getString("password", null);
        return results;
    }

    public static void setRevealEnable( boolean flag) {
        SharedPreferences.Editor prefs = mContext.getSharedPreferences(PREF_LOGIN, 0).edit();
        prefs.putBoolean("reveal_enable", flag);
        prefs.commit();
    }

    public static boolean isRevealEnable(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences(PREF_LOGIN, 0);
        return prefs.getBoolean("reveal_enable", false);
    }

    public static void setStatusEnable( boolean flag) {
        if (Build.VERSION.SDK_INT >= 19) {
            SharedPreferences.Editor prefs = mContext.getSharedPreferences(PREF_LOGIN, 0).edit();
            prefs.putBoolean("status_enable", flag);
            prefs.commit();
        }
    }

    public static boolean isStatusEnable(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences(PREF_LOGIN, 0);
        return prefs.getBoolean("status_enable", true) && Build.VERSION.SDK_INT >= 19;
    }

    public static void saveAutoLogin( String save) {
        SharedPreferences.Editor prefs = mContext.getSharedPreferences(PREF_LOGIN, 0).edit();
        prefs.putString("save", save);
        prefs.commit();
    }

    public static String loadAutoLogin(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences(PREF_LOGIN, 0);
        String results;
        results = prefs.getString("save", "yes");
        return results;
    }

    public static void savePassword( String save) {
        SharedPreferences.Editor prefs = mContext.getSharedPreferences(PREF_LOGIN, 0).edit();
        prefs.putString("save", save);
        prefs.commit();
    }

    public static String loadSavePassword(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences(PREF_LOGIN, 0);
        String results;
        results = prefs.getString("save", "yes");
        return results;
    }

    public static void saveKeyword( Set<String> keywords) {
        SharedPreferences.Editor prefs = mContext.getSharedPreferences(SEARCHKEYWORD, 0).edit();
        prefs.putStringSet("keyword", keywords);
        prefs.commit();
    }

    public static Set<String> loadKeyword(Context mContext) {
        SharedPreferences prefs = mContext
                .getSharedPreferences(SEARCHKEYWORD, 0);
        Set<String> results;
        results = prefs.getStringSet("keyword", null);
        return results;
    }

    /***************************
     * Setting
     ***************************************/
    private static final String SETTING = "setting";
    /****************************
     * WARN
     **************************************/
    private static final String WARN = "warning";

    public static boolean loadWarningSound(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences(SETTING, 0);
        return prefs.getBoolean(WARN, true);
    }

    public static void saveWarningSound(boolean arg1) {
        SharedPreferences.Editor prefs = mContext.getSharedPreferences(SETTING, 0).edit();
        prefs.putBoolean(WARN, arg1);
        prefs.commit();
    }

    /**
     * SVG
     */

    private static final String SVG = "svg";
    public static  void saveSVGInfo(String deviceCode,String fileCode,String createDate){
        SharedPreferences.Editor prefs = mContext.getSharedPreferences(SVG, 0).edit();
        prefs.putString(deviceCode+"_code", fileCode);
        prefs.putString(deviceCode+"_date", createDate);
        prefs.commit();
    }
    public static  String[] loadSVGInfo(String deviceCode){
        SharedPreferences prefs = mContext.getSharedPreferences(SVG, 0);
        String[] results = new String[2];
        results[0] = prefs.getString(deviceCode+"_code", null);
        results[1] = prefs.getString(deviceCode+"_date", null);
        return results;
    }

}
